package cfgs.gestudent.finestra.anyacademic;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cfgs.gestudent.dao.anyacademic.InserirAnyAcademic;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

import javax.swing.JLabel;
import javax.swing.JTextField;

import org.freixas.jcalendar.JCalendarCombo;
import javax.swing.SwingConstants;

public class FinestraAdministrarAnysAcademics extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7653126206112106929L;

	private final JPanel contentPanel = new JPanel();
	
	private ConfiguracionsIdiomes conf = new ConfiguracionsIdiomes();
	private ResourceBundle rb = conf.getIdioma();
	private JTextField textField;
	
	private JCalendarCombo calIni;
	private JCalendarCombo calFi;
	private JTextField textField_1;
	private JTextField textField_2;
	
	private String fechaInici;
	private String fechaFi;
	
	private Date dateInici;
	private Date dateFi;
	/**
	 * Create the dialog.
	 */
	public FinestraAdministrarAnysAcademics() {
		setResizable(false);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		setTitle(rb.getString("titolFinestraAnys"));
		
		JLabel lblNom = new JLabel(rb.getString("lblNomAcademic"));
		lblNom.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNom.setBounds(51, 70, 117, 14);
		contentPanel.add(lblNom);
		
		textField = new JTextField();
		textField.setBounds(178, 67, 197, 20);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblDataInci = new JLabel(rb.getString("lblDataIniAA"));
		lblDataInci.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDataInci.setBounds(51, 95, 117, 14);
		contentPanel.add(lblDataInci);
		
		calIni = new JCalendarCombo();
		calIni.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dateInici = calIni.getDate();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				fechaInici = sdf.format(dateInici);				
			}
		});
		calIni.setBounds(178, 92, 197, 20);
		contentPanel.add(calIni);
		
		JLabel lblDataFi = new JLabel(rb.getString("lblDataFiAA"));
		lblDataFi.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDataFi.setBounds(51, 120, 117, 14);
		contentPanel.add(lblDataFi);
		
		calFi = new JCalendarCombo();
		calFi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dateFi = calFi.getDate();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				fechaFi = sdf.format(dateFi);
			}
		});
		calFi.setBounds(178, 120, 197, 20);
		contentPanel.add(calFi);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(rb.getString("btnGuardar"));
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						String nom = textField.getText();
						boolean noErrors = false;
						if(nom==null | nom.equals("")){
							JOptionPane.showMessageDialog(null, rb.getString("faltenCampsObligatoris"), rb.getString("titolFaltenCamps"), JOptionPane.ERROR_MESSAGE);
							noErrors=false;
						}else{
							noErrors=true;
							if(fechaInici == null){
								JOptionPane.showMessageDialog(null, rb.getString("missatgeIniciNul"), rb.getString("titolIniciNul"), JOptionPane.ERROR_MESSAGE);
								noErrors=false;
							}else{
								noErrors = true;
								if(fechaFi == null){
									JOptionPane.showMessageDialog(null, rb.getString("missatgeFiNul"), rb.getString("titolFiNul"), JOptionPane.ERROR_MESSAGE);
									noErrors=false;
								}else{
									noErrors = true;
									if(dateInici.before(dateFi)){
										noErrors = true;
									}else{
										JOptionPane.showMessageDialog(null, rb.getString("missatgeFechesIncorr"), rb.getString("titolFechesIncorr"), JOptionPane.ERROR_MESSAGE);
										noErrors=false;
									}
								}
							}
						}
						
						
						
						
						
						if(noErrors == true){
							InserirAnyAcademic iaa = new InserirAnyAcademic();
							boolean inserit = iaa.inserirAnyAcademic(nom, fechaInici, fechaFi);
							if(inserit == true){
								JOptionPane.showMessageDialog(null,
										rb.getString("missatgeDadesInsertades"),
										rb.getString("titolDadesInsertades"),
										JOptionPane.INFORMATION_MESSAGE);
								textField.setText(null);
								dispose();
							}else{
								JOptionPane.showMessageDialog(null,
										rb.getString("missatgeDadesNoInsertades"),
										rb.getString("titolDadesNoInsertades"),
										JOptionPane.WARNING_MESSAGE);
								textField.setText(null);
							}
						}
						
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton(rb.getString("btnCancelar"));
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
